﻿Imports System.Data
Imports System.ComponentModel
Imports System.Reflection
Imports System.Windows.Media
Imports Global_Visas_Business_Layer
Public Class GeneralTasks

    Public Shared Sub ChangeTheme(ByVal form As System.Windows.Window)
        Try
            form.Resources.Clear()
            Dim rd As New ResourceDictionary()
            Dim style As String = My.Settings.CurrentTheme
            If style = "Aero.Normal" Then
                rd.Source = New Uri("/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/Aero.normalcolor.xaml", UriKind.Relative)

            ElseIf style = "Luna.Normal" Then
                rd.Source = New Uri("/PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component/themes/luna.normalcolor.xaml", UriKind.Relative)

            ElseIf style = "Luna.Homestead" Then
                rd.Source = New Uri("/PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component/themes/luna.Homestead.xaml", UriKind.Relative)

            ElseIf style = "Luna.Metallic" Then
                rd.Source = New Uri("/PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component/themes/luna.Metallic.xaml", UriKind.Relative)

            ElseIf style = "Classic" Then
                rd.Source = New Uri("/PresentationFramework.Classic;V3.0.0.0;31bf3856ad364e35;component/themes/Classic.xaml", UriKind.Relative)

            ElseIf style = "Royale.Normal" Then
                rd.Source = New Uri("/PresentationFramework.Royale;V3.0.0.0;31bf3856ad364e35;component/themes/Royale.normalcolor.xaml", UriKind.Relative)

            Else
                rd.Source = New Uri("/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/Aero.normalcolor.xaml", UriKind.Relative)

            End If
            If Not rd.Source Is Nothing Then
                form.Resources.MergedDictionaries.Add(rd)
                form.WindowStartupLocation = WindowStartupLocation.CenterOwner
            End If
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "GeneralTasks-ChangeTheme", False)
        End Try
    End Sub

    Public Shared Sub ChangeTheme(ByVal form As System.Windows.Controls.UserControl)
        Try
            form.Resources.Clear()
            Dim rd As New ResourceDictionary()
            Dim style As String = My.Settings.CurrentTheme
            If style = "Aero.Normal" Then
                rd.Source = New Uri("/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/Aero.normalcolor.xaml", UriKind.Relative)

            ElseIf style = "Luna.Normal" Then
                rd.Source = New Uri("/PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component/themes/luna.normalcolor.xaml", UriKind.Relative)

            ElseIf style = "Luna.Homestead" Then
                rd.Source = New Uri("/PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component/themes/luna.Homestead.xaml", UriKind.Relative)

            ElseIf style = "Luna.Metallic" Then
                rd.Source = New Uri("/PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component/themes/luna.Metallic.xaml", UriKind.Relative)

            ElseIf style = "Classic" Then
                rd.Source = New Uri("/PresentationFramework.Classic;V3.0.0.0;31bf3856ad364e35;component/themes/Classic.xaml", UriKind.Relative)

            ElseIf style = "Royale.Normal" Then
                rd.Source = New Uri("/PresentationFramework.Royale;V3.0.0.0;31bf3856ad364e35;component/themes/Royale.normalcolor.xaml", UriKind.Relative)

            Else
                rd.Source = New Uri("/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/Aero.normalcolor.xaml", UriKind.Relative)

            End If
            If Not rd.Source Is Nothing Then
                form.Resources.MergedDictionaries.Add(rd)
            End If
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "GeneralTasks-ChangeTheme", False)
        End Try
    End Sub

    Public Shared Sub ChangeTheme(ByVal form As System.Windows.Controls.Page)
        Try
            form.Resources.Clear()
            Dim rd As New ResourceDictionary()
            Dim style As String = My.Settings.CurrentTheme
            If style = "Aero.Normal" Then
                rd.Source = New Uri("/PresentationFramework.Classic;V3.0.0.0;31bf3856ad364e35;component/themes/Aero.normalcolor.xaml", UriKind.Relative)

            ElseIf style = "Luna.Normal" Then
                rd.Source = New Uri("/PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component/themes/luna.normalcolor.xaml", UriKind.Relative)

            ElseIf style = "Luna.Homestead" Then
                rd.Source = New Uri("/PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component/themes/luna.Homestead.xaml", UriKind.Relative)

            ElseIf style = "Luna.Metallic" Then
                rd.Source = New Uri("/PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component/themes/luna.Metallic.xaml", UriKind.Relative)

            ElseIf style = "Classic" Then
                rd.Source = New Uri("/PresentationFramework.Classic;V3.0.0.0;31bf3856ad364e35;component/themes/Classic.xaml", UriKind.Relative)

            ElseIf style = "Royale.Normal" Then
                rd.Source = New Uri("/PresentationFramework.Royale;V3.0.0.0;31bf3856ad364e35;component/themes/Royale.normalcolor.xaml", UriKind.Relative)

            Else
                rd.Source = New Uri("/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/Aero.normalcolor.xaml", UriKind.Relative)

            End If
            If Not rd.Source Is Nothing Then
                form.Resources.MergedDictionaries.Add(rd)
            End If
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "GeneralTasks-ChangeTheme", False)
        End Try
    End Sub

    Public Shared Sub LoopThroughTheChildren(ByVal obj As Grid)
        Try

            Dim t As Type = GetType(Brushes)
            obj.Background = t.InvokeMember(My.Settings.CurrentColor, BindingFlags.GetProperty, Nothing, Nothing, Nothing, Nothing)
            For currentControlIndex As Integer = 0 To obj.Children.Count - 1
                'MsgBox(obj.Children(currentControlIndex).GetType().Equals(GetType(Image)))
                'If currentControlIndex = 32 Then
                If obj.Children(currentControlIndex).GetType().Equals(GetType(Image)) Then
                    Continue For
                Else
                    Dim currentControl As Control = CType(obj.Children(currentControlIndex), Control)
                    ReColorTheControls(currentControl)
                End If
            Next
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "GeneralTasks-LoopThroughTheChildren", False)
        End Try
    End Sub

    Private Shared Sub ReColorTheControls(ByVal currentControl As Control)
        Try
            Dim controlType As Type = currentControl.GetType()
            Dim t As Type = GetType(Brushes)
            If controlType.Equals(GetType(Label)) Then
                currentControl.Background = t.InvokeMember(My.Settings.CurrentColor, BindingFlags.GetProperty, Nothing, Nothing, Nothing, Nothing)
            ElseIf controlType.Equals(GetType(TextBox)) Then
                If currentControl.Name = "lblPrivateMessage" Then
                    currentControl.Background = t.InvokeMember(My.Settings.CurrentColor, BindingFlags.GetProperty, Nothing, Nothing, Nothing, Nothing)
                End If
            ElseIf controlType.Equals(GetType(TabControl)) Then
                currentControl.Background = t.InvokeMember(My.Settings.CurrentColor, BindingFlags.GetProperty, Nothing, Nothing, Nothing, Nothing)
                Dim tabc As TabControl = CType(currentControl, TabControl)
                For Each currentTabItem As TabItem In tabc.Items
                    For Each currentItem As Object In LogicalTreeHelper.GetChildren(currentTabItem)
                        If currentItem.GetType().Equals(GetType(Grid)) Then
                            Dim currentGrid As Grid = CType(currentItem, Grid)
                            LoopThroughTheChildren(currentGrid)
                        End If
                    Next
                Next
            ElseIf controlType.Equals(GetType(GroupBox)) Then
                For Each currentSubcontrol As Object In LogicalTreeHelper.GetChildren(CType(currentControl, GroupBox))
                    If currentSubcontrol.GetType().Equals(GetType(Grid)) Then
                        LoopThroughTheChildren(CType(currentSubcontrol, Grid))
                    End If
                Next
            End If
            Select Case My.Settings.CurrentColor
                Case "Black"
                    CheckandFillpecialColors(currentControl)
                Case Else
                    CheckandFillpecialColors(currentControl)
            End Select

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "GeneralTasks-ReColorTheControls", False)
        End Try
    End Sub

    Private Shared Sub CheckandFillpecialColors(ByVal currentControl As Control)
        Try
            Dim t As Type = GetType(Brushes)
            Dim currentBrush As Brush
            Select Case My.Settings.CurrentColor
                Case "Black"
                    currentBrush = t.InvokeMember("Blue", BindingFlags.GetProperty, Nothing, Nothing, Nothing, Nothing)
                    currentControl.Foreground = currentBrush
                Case Else
                    currentBrush = t.InvokeMember("Black", BindingFlags.GetProperty, Nothing, Nothing, Nothing, Nothing)
                    currentControl.Foreground = currentBrush
            End Select
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "GeneralTasks-CheckandFillpecialColors", False)
        End Try
    End Sub

    Public Shared Sub EnumerateandFill(ByVal enumerator As IEnumerator)
        Try
            While enumerator.MoveNext
                Dim currentGrid As Grid = CType(enumerator.Current, Grid)
                Dim t As Type = GetType(Brushes)
                If My.Settings.CurrentColor <> String.Empty Then
                    Dim currentBrush As Brush = t.InvokeMember(My.Settings.CurrentColor, BindingFlags.GetProperty, Nothing, Nothing, Nothing, Nothing)
                    currentGrid.Background = currentBrush
                    GeneralTasks.LoopThroughTheChildren(currentGrid)
                Else
                    'MsgBox("There is no color selected and saved by you! Please select it from the top right of main screen.")
                    Exit While
                End If
            End While
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "GeneralTasks-EnumerateandFill", False)
        End Try
    End Sub

    Public Shared Sub FillComboBoxWithSystemColorList(ByVal cmbBox As ComboBox)
        Try
            Dim brushTypes As Type = GetType(Brushes)
            Dim colorList As New DataTable
            Dim columnColorType As DataColumn = New DataColumn("ColorType")
            colorList.Columns.Add(columnColorType)
            For Each propertyInfo As PropertyInfo In brushTypes.GetProperties(BindingFlags.Public Or BindingFlags.Static)
                If propertyInfo.PropertyType.Equals(GetType(SolidColorBrush)) Then
                    Dim colorRow As DataRow = colorList.NewRow()
                    colorRow.BeginEdit()
                    colorRow("ColorType") = propertyInfo.Name
                    colorRow.EndEdit()
                    colorList.Rows.Add(colorRow)
                End If
            Next
            cmbBox.ItemsSource = CType(colorList, IListSource).GetList
            cmbBox.DisplayMemberPath = "ColorType"
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "GeneralTasks-FillComboBoxWithSystemColorList", False)
        End Try
    End Sub

End Class
